﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ConfApp.Models;
using Newtonsoft.Json;

namespace ConfApp.Services.Podcasts
{
    public class PodcastStore : Store, IPodcastStore
    {
        private const string PodcastsStore = "PodcastsStore";
        
        private Dictionary<string,Podcast> _podcasts;
        private int _maxId = 0;
        
        public PodcastStore()
        {
            //DeleteStore(TwittsStore);
            Initialize();
        }

        public int Version
        {
            get { return _maxId; }
        }


        public IEnumerable<Podcast> GetPodcasts()
        {
            return _podcasts.Values.OrderByDescending(p => p.Date);
        }

        public Podcast FindPodcast(int id)
        {
            return _podcasts.Values.FirstOrDefault(p => p.ID == id);
        }

        public int SavePodcasts(IEnumerable<Podcast> podcasts)
        {
            var count = 0;
            //_podcasts.Clear();
            foreach (var podcast in podcasts)
            {
                if(!_podcasts.ContainsKey(podcast.Url))
                {
                    podcast.ID = ++_maxId;
                    _podcasts.Add(podcast.Url,podcast);
                    count++;
                }
                else
                {
                    podcast.ID = _podcasts[podcast.Url].ID;
                    _podcasts[podcast.Url] = podcast;
                }
            }
            SaveStore();
            return count;
        }

        public void SaveStore()
        {
            var podcastBatch = new PodcastBatch()
            {
                Podcasts = _podcasts.Values.OrderByDescending(t=>t.Date).ToList()
            };
            StoreDataToJson(PodcastsStore, podcastBatch);
        }

        private void Initialize()
        {
            var podcastBatch = GetDataFromJson<PodcastBatch>(PodcastsStore);
            var podcasts = podcastBatch.Podcasts;
            if(podcasts.Count == 0)
            {
                podcasts = LoadDefaultData().Podcasts;
            }
            _podcasts = new Dictionary<string, Podcast>();
            foreach (var podcast in podcasts)
            {
                _podcasts.Add(podcast.Url,podcast);
                if (podcast.ID > _maxId) _maxId = podcast.ID;
            }
        }

        private PodcastBatch LoadDefaultData()
        {
            using (var str = new StreamReader(Application.GetResourceStream(new Uri("/ConfApp;component/Resources/Data/podcast-data.json", UriKind.Relative)).Stream))
            {
                return JsonConvert.DeserializeObject<PodcastBatch>(str.ReadToEnd());
            }
        }
    }
}
